package com.yoda.youdianda.advice;

import com.yoda.youdianda.common.R;
import com.yoda.youdianda.exception.BusinessException;
import com.yoda.youdianda.exception.RegException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 统一异常处理类
 *
 * @author lenovo
 * @date 2022/11/12
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    public R businessException(BusinessException be) {
        return R.error(be.getMessage(), be.getErrorResult().getErrorCode());
    }

    @ExceptionHandler(NullPointerException.class)
    public R nullException() {
        return R.error("数据异常");
    }

    @ExceptionHandler(RegException.class)
    public R RegException(RegException be) {
        return R.error(be.getErrorResult().getErrorMessage(), be.getErrorResult().getErrorCode());
    }
}
